package com.servlet;

import com.pojo.User;
import com.service.user.UserService;
import com.service.user.UserServiceImpl;
import com.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userPhone = req.getParameter("userPhone");
        String userPassword = req.getParameter("userPassword");
        System.out.println("userPhone为："+userPhone+"/n userPassword为："+userPassword);

        UserService userService = new UserServiceImpl();
        User user = userService.Login(userPhone, userPassword);

        if(user!=null && userPassword.equals(user.getUserPassword())){
            req.getSession().setAttribute(Constant.USER_SESSION,user);
            /*跳转*/
            req.getRequestDispatcher("/jsp/allgoods.jsp").forward(req,resp);
        }else{
            String msg = "用户名或者密码错误";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
